﻿@model GiveCampStarterKit.Website.Areas.TeamAdministration.Models.Home.EditViewModel
@{
    ViewBag.Title = "Edit";
}

<h2>Edit Team "@Model.Team.Name"</h2>

<div id="NameEdit">
    <label for="Name">Name</label>
    @Html.TextBoxFor(m => m.Team.Name)
    <input id="UpdateTeamName" value="Update" type="button" />
</div>

<h3>Users In This Group</h3>
<div id="volunteersInTeam">
</div>


<h3>Other Users</h3>
<div id="volunteersNotInTeam">
</div>

<p><a href="/teamadministration/home">Back to the teams list</a></p>

<script type="text/javascript">

    var teamId = @Model.Team.Id;

    $(document).ready(function () {

        loadVolunteersInTeam();
        loadVolunteersNotInTeam();

        $('#UpdateTeamName').click(function() {
            var teamName = $('#Team_Name').val();

            $.ajax({
                data: { teamId: teamId, teamName: teamName },
                url: '/teamadministration/home/changeteamname',
                type: 'POST',
                success: function()
                {
                    alert('Team name updated.');
                }
            });

            
        });
    });

    function loadVolunteersInTeam() {
        $.get('/teamadministration/home/GetVolunteersInTeam/@Model.Team.Id', null, function (data) {
            $('#volunteersInTeam').html(data);

            $('.removeButton').click(function () {
                var volunteerId = $(this).attr('rel');

                $.ajax({
                    data: { teamId: teamId, volunteerId: volunteerId },
                    url: '/teamadministration/home/removevolunteerfromteam',
                    type: 'POST',
                    success: function()
                    {
                        loadVolunteersInTeam();
                        loadVolunteersNotInTeam();
                    }
                });
            });
        });
    }

    function loadVolunteersNotInTeam() {
        $.get('/teamadministration/home/GetVolunteersNotInTeam/@Model.Team.Id', null, function (data) {
            $('#volunteersNotInTeam').html(data);

            $('.addButton').click(function () {
                var volunteerId = $(this).attr('rel');

                $.ajax({
                    data: { teamId: teamId, volunteerId: volunteerId },
                    url: '/teamadministration/home/addvolunteertoteam',
                    type: 'POST',
                    success: function()
                    {
                        loadVolunteersInTeam();
                        loadVolunteersNotInTeam();
                    }
                });
            });
        });
    }

</script>